/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lwjglbase;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL11.*;
import org.newdawn.slick.opengl.InternalTextureLoader;
import org.newdawn.slick.opengl.Texture;

/**
 *
 * @author 202429
 */
public class Player {

    float x = 0;
    float y = 0;
    float z = 0;
    static FloatBuffer vertBuffer, normalBuffer, texCoordBuffer;
    static Texture tex;
    static String modelPath = LWJGLbase.MODELS_PATH + "test.obj";

    public Player() {
    }

    public static void init() {
        loadModel();
        loadTex();
    }

    static void loadTex() {
        try {
            tex = InternalTextureLoader.get().getTexture(LWJGLbase.SPRITES_PATH + "monkeyTex.png", false, GL_LINEAR);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * Loads on obj model. This REQUIRES a properly formed OBJ file and will
     * probably crash in really ugly ways if it doesn't get a good file. Files 
     * *MUST* have normals AND texture coordinates. Also, files *MUST* be
     * triangulated, this wont work with quads. Can use lots of mem when 
     * loading big models. (multiple MB), so make sure to run with -Xmx and
     * -Xms adjusted accordingly
     */
    static void loadModel() {
        //declare these in here so they get garbage collected
        ArrayList<float[]> vertices = new ArrayList<float[]>();
        ArrayList<float[]> normals = new ArrayList<float[]>();
        ArrayList<float[]> texCoords = new ArrayList<float[]>();
        ArrayList<String> faces = new ArrayList<String>();

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(modelPath)));
            while ((modelPath = br.readLine()) != null) {
                if (modelPath.startsWith("v ")) {
                    vertices.add(Utils.stringToFloatArray(modelPath.substring(2), " "));
                } else if (modelPath.startsWith("vn ")) {
                    normals.add(Utils.stringToFloatArray(modelPath.substring(3), " "));
                } else if (modelPath.startsWith("vt ")) {
                    texCoords.add(Utils.stringToFloatArray(modelPath.substring(3), " "));
                } else if (modelPath.startsWith("f ")) {
                    faces.add(modelPath.substring(2));
                }
            }
        } catch (Exception e) {
            System.out.println("Error loading model file.");
            e.printStackTrace();
        }

        //create the actual buffers
        vertBuffer = BufferUtils.createFloatBuffer(faces.size() * 9);
        normalBuffer = BufferUtils.createFloatBuffer(faces.size() * 9);
        texCoordBuffer = BufferUtils.createFloatBuffer(faces.size() * 6);

        //parse the face strings
        for (String face : faces) {
            String[] verts = face.split(" ");
            for (String vert : verts) {
                String[] str = vert.split("/");
                vertBuffer.put(vertices.get(Integer.parseInt(str[0]) - 1));
                normalBuffer.put(normals.get(Integer.parseInt(str[2]) - 1));
                texCoordBuffer.put(texCoords.get(Integer.parseInt(str[1]) - 1));
            }
        }
        vertBuffer.rewind();
        texCoordBuffer.rewind();
        normalBuffer.rewind();
    }

    public void draw() {
        tex.bind();
        glVertexPointer(3, 0, vertBuffer);
        glNormalPointer(0, normalBuffer);
        glTexCoordPointer(2, 0, texCoordBuffer);
        
        glPushMatrix();
        glTranslatef(x, y, z);
        glDrawArrays(GL_TRIANGLES, 0, vertBuffer.capacity() / 3);
        glPopMatrix();
    }
}